<template>
  <el-form ref="form" :label-position="device === 'mobile' ? 'top' : 'right'" label-width="100px" :model="form" :rules="rules">
    <el-form-item label="物种" prop="classCode">
        <el-input v-model.trim="form.classCode" autocomplete="off" placeholder="请输入物种" />
      </el-form-item>
      <el-form-item label="类别" prop="category">
        <el-input v-model.trim="form.category" autocomplete="off" placeholder="请输入类别" />
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model.trim="form.name" autocomplete="off" placeholder="请输入产品名称" />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input v-model.trim="form.specification" autocomplete="off" placeholder="请输入规格" />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model.trim="form.model" autocomplete="off" placeholder="请输入型号" />
      </el-form-item>
      <el-form-item label="采购" prop="purchase">
        <el-input v-model.trim="form.purchase" autocomplete="off" placeholder="请输入采购" />
      </el-form-item>
      <el-form-item label="库管" prop="worker">
        <el-input v-model.trim="form.worker" autocomplete="off" placeholder="请输入库管" />
      </el-form-item>
      <el-form-item label="数量" prop="num">
        <el-input v-model.number="form.num" autocomplete="off" placeholder="请输入数量" type="number" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model.trim="form.unit" autocomplete="off" placeholder="请输入单位" />
      </el-form-item>
      <el-form-item label="失效日期" prop="endTime">
        <el-date-picker v-model="form.endTime" placeholder="请选择失效日期" style="width: 100%" value-format="timestamp" />
      </el-form-item>
    <el-form-item v-if="device === 'mobile'" class="submit-content">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</template>
  
<script>
import { mapGetters } from 'vuex'
import { API_goodsCreate, API_goodsEdit } from '@/api/goods'
export default {
  name: 'FormEdit',
  props: {
    formData: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      form: {
          classCode: '',
          category: '',
          name: '',
          specification: '',
          model: '',
          purchase: '',
          water: '',
          worker: '',
          num: '',
          unit: '',
          endTime: '',
          direction: 1,
        },
        rules: {
          classCode: [{ required: true, trigger: 'blur', message: '请输入物种' }],
          category: [{ required: true, trigger: 'blur', message: '请输入类别' }],
          name: [{ required: true, trigger: 'blur', message: '请输入产品名称' }],
          specification: [{ required: true, trigger: 'blur', message: '请输入规格' }],
          model: [{ required: true, trigger: 'blur', message: '请输入型号' }],
          num: [{ required: true, trigger: 'blur', message: '请输入数量' }],
        },
    }
  },
  computed: {
    ...mapGetters({
      device: 'settings/device',
    }),
  },
  watch: {
    'formData': {
      handler(newData) {
        if (newData) {
          this.form = Object.assign({}, this.formData)
          this.form.direction = 1
        }
      },
      immediate: true,
    },
  },
  methods: {
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      if (this.device === 'mobile') {
        this.$router.push({
          name: 'InWarehouse',
          params: { refresh: true },
        })
      }
    },
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if (this.form.code) {
            const { message } = await API_goodsEdit(this.form)
            this.$baseMessage(message, 'success')
          } else {
            const { message } = await API_goodsCreate(this.form)
            this.$baseMessage(message, 'success')
          }
          this.$emit('fetch-data')
          this.close()
        } else {
          return false
        }
      })
    },
  },
}
</script>
  